package com.ffcs.web.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.jboss.logging.NDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ffcs.web.model.dto.BaseResponse;
import com.ffcs.web.model.dto.CityNameResponse;
import com.ffcs.web.model.dto.CityResponse;
import com.ffcs.web.services.MapService;

@Controller
public class MapController {

	private Logger logger = Logger.getLogger(CounseController.class);
	
	@Autowired
	private MapService mapService;
	
	/**
	 * 根据经纬度获取城市名称接口
	 * @return
	 */
	@RequestMapping("getCityName")
	@ResponseBody
	public CityNameResponse getCityName(HttpServletRequest request, String la,String lo){
		logger.info("la="+la+",lo="+lo);
		CityNameResponse cityNameResponse=new CityNameResponse();
		cityNameResponse.setResultCode("0");
		cityNameResponse.setResultDesc("success");
		
		if(null!=request.getSession().getAttribute("cityName") && !"".equals(request.getSession().getAttribute("cityName"))){
			logger.info("已经定为过，直接返回");
			String cityName=(String) request.getSession().getAttribute("cityName");
			cityNameResponse.setCityName(cityName);
		}else{
			cityNameResponse.setCityName(mapService.getCityName(la,lo));
		}
		request.getSession().setAttribute("cityName",mapService.getCityName(la,lo));
		
		return cityNameResponse;
	}
	
	@RequestMapping("getCityList")
	@ResponseBody
	public CityResponse getCityList(HttpServletRequest request){
		NDC.push("获取城市列表");
		CityResponse cityNameResponse=new CityResponse();
		cityNameResponse.setResultCode("0");
		cityNameResponse.setResultDesc("success");
		
		List<CityNameResponse> cityNameList=mapService.getCityList();
		cityNameResponse.setCityNameList(cityNameList);
		return cityNameResponse;
	}
	
	/*8
	 * 判断是否已经定位
	 */
	@RequestMapping("isDingWei")
	@ResponseBody
	public BaseResponse isDingWei(HttpServletRequest request){
		BaseResponse baseResponse=new BaseResponse();
		
		if(null!=request.getSession().getAttribute("cityName")){
			baseResponse.setResultCode("0");
			baseResponse.setResultDesc("success");
			return baseResponse;
		}else{
			baseResponse.setResultCode("-1");
			logger.info("用户未定位");
			baseResponse.setResultDesc("failure");
			return baseResponse;
		}
	}
	
	/*8
	 * 判断是否已经定位
	 */
	@RequestMapping("saveConType")
	@ResponseBody
	public BaseResponse saveConType(HttpServletRequest request,String contype){
		BaseResponse baseResponse=new BaseResponse();
		request.getSession().setAttribute("yuyueContype",contype);
		baseResponse.setResultCode("0");
		baseResponse.setResultDesc("success");
		return baseResponse;
	}
}
